/** 商品数据 */
export const ruleFormData = {
	/*** 商品名称 */
	name: '',
	/*** 商品卖点 */
	sellingPoints: '',
	/*** 原价 */
	originalPrice: '',
	/*** 现价 */
	price: '',
	/*** 销量 */
	sales: '',
	/*** 库存 */
	stock: '',
	/*** 分类id */
	categoryId: [],
	/*** 商品类型 */
	goodsType: 1,
	/*** 商品图片 */
	image: '',
	/*** 商品轮播图 */
	bannerImage: [],
	/*** 商品详情 */
	content: '',
	/*** 售后天数 */
	afterSaleDays: 0,
	/*** 售后信息 */
	afterSaleInfo: '',
	/*** 商品规格 */
	specification: [],
	/*** 是否上架 */
	isActive: true
}
/**
 * 规则数据
 */
export const rulesData = {
	name: [
		{ required: true, message: '请输入商品名称', trigger: 'blur' },
	],
	sellingPoints: [
		{ required: true, message: '请输入商品卖点', trigger: 'blur' },
	],
	originalPrice: [
		{ required: true, message: '请输入商品原价', trigger: 'blur' },
	],
	price: [
		{ required: true, message: '请输入商品售价', trigger: 'blur' },
	],
	sales: [
		{ required: true, message: '请输入商品销量', trigger: 'blur' },
	],
	stock: [
		{ required: true, message: '请输入商品库存', trigger: 'blur' },
	],
	categoryId: [
		{ required: true, message: '请选择商品分类', trigger: 'change' },
	],
	goodsType: [
		{ required: true, message: '请选择商品类型', trigger: 'change' },
	],
	image: [
		{ required: true, message: '请上传商品主图', trigger: 'change' },
	],
	bannerImage: [
		{ required: true, message: '请上传商品轮播图', trigger: 'change' },
	],
	content: [
		{ required: true, message: '请输入商品详情', trigger: 'blur' },
	],
	afterSaleDays: [
		{ required: true, message: '请输入售后天数', trigger: 'blur' },
	],
	afterSaleInfo: [
		{ required: true, message: '请输入售后信息', trigger: 'blur' },
	]
}
export const propsData = {
	title: {
		type: String,
		default: '添加商品'
	},
	modelValue: {
		default: false,
		type: Boolean,
		required: true
	},
	categoryList: {
		default: () => [],
		type: Array
	}
}


export interface FrontendSpec {
	[key : string] : string | number;
	originalPrice : number;
	price : number;
	sales : number;
	stock : number;
}

export interface GoodsSpecification {
	specName : string;
	specValue : string;
	price : number;
	originalPrice ?: number;
	stock : number;
	sales : number;
	image ?: string; // 如果有图片信息可以填充
}
/**
 * 将前端规格数据转换为后端规格数据
 * @param specifications 前端规格数据
 * @returns 后端规格数据
 */
export const convertToGoodsSpecifications = (specifications : FrontendSpec[]) : GoodsSpecification[] => {
	const result : GoodsSpecification[] = [];
	specifications.forEach(spec => {
		const specKeys = Object.keys(spec);
		specKeys.forEach(key => {
			if (key !== 'originalPrice' && key !== 'price' && key !== 'sales' && key !== 'stock') {
				result.push({
					specName: key,
					specValue: spec[key] as string,
					price: spec.price,
					originalPrice: spec.originalPrice || undefined,
					stock: spec.stock,
					sales: spec.sales,
					image: '', // 如果有图片信息可以填充
				});
			}
		});
	});
	return result;
};
export const zhuanhuan = (specifications:any) => {
  return specifications.map(spec => {
    // 动态组合规格项，排除非规格的字段
    const specKeys = Object.keys(spec).filter(key => !['price', 'originalPrice', 'stock', 'sales'].includes(key));

    // 组合规格项名称，例如 "红色-L-128GB"
    const specName = specKeys.map(key => spec[key]).join('-');

    // 将规格项名称用逗号连接，例如 "颜色,尺寸,内存"
    const specValue = specKeys.join(',');

    return {
      specName,
      price: parseFloat(spec.price as string), // 将价格字段转为浮点型
      originalPrice: parseFloat(spec.originalPrice as string), // 同样处理原价
      stock: parseInt(spec.stock as string), // 将库存字段转为整型
      sales: parseInt(spec.sales as string), // 将销量字段转为整型
      image: "", // 可以根据需要设置图片
      specValue // 存储规格项的名称，用逗号分隔
    };
  });
};